<?php
/*
Author: Le Phu Qui
*/
ob_start();
global $wpdb, $hitcounter_table_name;
if (!function_exists("get_option")) {hitcounter_readme();die;}
$hitcounter_table_name = $wpdb->prefix . "soluongtruycap";
$table_count_online = $wpdb->prefix."soluongonline";
## dem so luong truy cap
function bt_soluongtruycap() {
	global $wpdb, $hitcounter_table_name;	 
	$hit_count = $wpdb->get_var($wpdb->prepare("select hit_count from $hitcounter_table_name"));
	$thesite = get_option('siteurl');
	
	if (!isset($_COOKIE['deplayf5'])&& is_home())
	{		
		setcookie( "deplayf5", $thesite, time()+600 ); 
		$hit_count =$hit_count + 1;		
		$wpdb->query("UPDATE $hitcounter_table_name SET hit_count = '$hit_count' WHERE site = '$thesite'");
	}
	$count=$wpdb->get_var($wpdb->prepare("SELECT hit_count FROM $hitcounter_table_name "));
	
    return $count;
}

## dem so luong online
function db_soluongonline()
{
	global $wpdb;
	$tg=time();
	$tgout=300;
	$tgnew=$tg - $tgout;	
	$wpdb->query("INSERT INTO wp_soluongonline(tgtmp,ip,local)VALUES($tg,'$_SERVER[REMOTE_ADDR]','$_SERVER[PHP_SELF]')");		
	$wpdb->query("delete from wp_soluongonline where tgtmp < $tgnew");
	$count_online = $wpdb->get_results("SELECT DISTINCT ip FROM wp_soluongonline");
	return count($count_online);
}
function hitcounter_readme() 
{
	echo '<br><strong>Something is wrong with Simple Hit Counter!</strong><br>';
}
function hitcounter_install() 
{
	global $wpdb, $hitcounter_table_name,$table_count_online;
	
	$thesite = get_option('siteurl');
	## create count accsess
	if ($wpdb->get_var("SHOW TABLES LIKE '$hitcounter_table_name'") != $hitcounter_table_name) 
	{
		$wpdb->query("CREATE TABLE $hitcounter_table_name (
			site TEXT NOT NULL,
			hit_count INT NOT NULL
		)");
		$wpdb->query("INSERT INTO $hitcounter_table_name (site, hit_count) VALUES ('$thesite', 0)");
		add_option("hitcounter_db_version", "1.0");
	}
	
	## create count online
	if ($wpdb->get_var("SHOW TABLES LIKE '$table_count_online'") != $table_count_online) 
	{
		$wpdb->query("CREATE TABLE $table_count_online (			
			tgtmp INT NOT NULL,
			ip VARCHAR(50) NOT NULL,
			local VARCHAR(100) NOT NULL,
			PRIMARY KEY(tgtmp)					
		)");
		//$wpdb->query("INSERT INTO $table_count_online (site, hit_count) VALUES ('$thesite', 0)");
		add_option("hitcounter_db_version", "1.0");
	}
}
add_action('init','hitcounter_install' );
?>
